import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:like_button/like_button.dart';

import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/mysenddongtaiModel.dart';
import 'package:qgchat/http/models/resModel.dart';
import 'package:qgchat/widgets/due/dif.dart';
import 'package:qgchat/widgets/dynamic/comment.dart';
import 'package:qgchat/widgets/dynamic/squareAvatar.dart';
import 'package:qgchat/widgets/dynamic/squareImgs.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';

import 'BloC/myDynamicBloC.dart';
import 'component/delBtn.dart';

class MyDynamicView extends StatefulWidget {
  MyDynamicView({Key key}) : super(key: key);

  @override
  _MyDynamicViewState createState() => _MyDynamicViewState();
}

class _MyDynamicViewState extends State<MyDynamicView> {
  @override
  void initState() {
    super.initState();
    BlocProvider.of<MyDynamicBloc>(context).add(MyDynamicEvent.refresh);
  }

  // 点赞
  Future<bool> like(bool isLiked, int id) async {
    ResModel res;
    if (isLiked) {
      res = await CommonApis.canceldtdz(id);
    } else {
      res = await CommonApis.dtdz(id);
    }
    if (res.status == 0) {
      return false;
    } else {
      return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(px(88)),
        child: AppBar(
          centerTitle: true,
          title: Text(
            "我的动态",
            style: TextStyle(
              fontSize: px(34),
              fontWeight: FontWeight.bold,
            ),
          ),
          actions: <Widget>[
            Container(
              width: px(125),
              child: FlatButton(
                onPressed: () {
                  showCupertinoModalPopup(
                    context: context,
                    builder: (BuildContext ctx) {
                      return CupertinoActionSheet(
                        title: Text("发布动态"),
                        message: Text("选择一个发帖形式"),
                        cancelButton: CupertinoActionSheetAction(
                          onPressed: () => Navigator.pop(ctx),
                          child: Text("取消"),
                        ),
                        actions: <Widget>[
                          CupertinoActionSheetAction(
                            onPressed: () async {
                              Navigator.pop(ctx);
                              goPage(ctx, '/releaseDynamic').then(
                                (res) {
                                  BlocProvider.of<MyDynamicBloc>(context)
                                      .add(MyDynamicEvent.refresh);
                                },
                              );
                            },
                            child: Text("图片动态"),
                          ),
                          CupertinoActionSheetAction(
                            onPressed: () async {
                              Navigator.pop(ctx);
                              goPage(ctx, '/releaseVideoDynamic').then(
                                (res) {
                                  BlocProvider.of<MyDynamicBloc>(context)
                                      .add(MyDynamicEvent.refresh);
                                },
                              );
                            },
                            child: Text("视频动态"),
                          ),
                        ],
                      );
                    },
                  );
                },
                padding: EdgeInsets.zero,
                child: Text(
                  "发布",
                  style: TextStyle(
                    color: col("5640c6"),
                    fontSize: px(34),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      body: BlocBuilder<MyDynamicBloc, List<MysenddongtaiModel>>(
        builder: (context, state) {
          return Container(
            color: col("f5f7fa"),
            child: EasyRefresh(
              header: EasyRefreshSetting.header,
              footer: EasyRefreshSetting.footer,
              onRefresh: () async {
                BlocProvider.of<MyDynamicBloc>(context)
                    .add(MyDynamicEvent.refresh);
              },
              onLoad: () async {
                BlocProvider.of<MyDynamicBloc>(context)
                    .add(MyDynamicEvent.load);
              },
              child: ListView.builder(
                itemCount: state.length,
                itemBuilder: (BuildContext context, int i) {
                  print(state[i]);
                  return Container(
                    color: Colors.white,
                    margin: EdgeInsets.only(bottom: px(15)),
                    padding: EdgeInsets.all(px(30)),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        // 头像 昵称 时间 删除按钮
                        Container(
                          height: px(90),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              // 头像 昵称 时间
                              Container(
                                child: Row(
                                  children: <Widget>[
                                    SquareAvatar(
                                      avatarUrl: "${state[i].memberHead}",
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(left: px(20)),
                                      padding: EdgeInsets.symmetric(
                                        vertical: px(7),
                                      ),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Text(
                                            "${state[i].memberNickname}",
                                            style: TextStyle(
                                              color: col("303133"),
                                              fontSize: px(30),
                                            ),
                                          ),
                                          Text(
                                            "${Date.simple(int.parse(state[i].createtime))}",
                                            style: TextStyle(
                                              color: col("bfc2cc"),
                                              fontSize: px(24),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              // 删除动态 按钮
                              DelBtn(state[i].id),
                            ],
                          ),
                        ),
                        // 配文
                        If(
                          dif: state[i].text != null,
                          ifView: Container(
                            margin: EdgeInsets.only(top: px(20)),
                            child: Text(
                              "${state[i].text}",
                              style: TextStyle(
                                color: col("303133"),
                                fontSize: px(30),
                              ),
                            ),
                          ),
                        ),
                        // 配图
                        if (state[i].img != null)
                          Container(
                            margin: EdgeInsets.only(top: px(20)),
                            child: SquareImgs(imgs: state[i].img),
                          ),
                        // 视频
                        if (state[i].video != null)
                          GestureDetector(
                            onTap: () {
                              goPage(
                                context,
                                "/videoPlay",
                                param: {"url": state[i].video},
                              );
                            },
                            child: Container(
                              constraints: BoxConstraints(
                                maxWidth: px(300),
                                maxHeight: px(300),
                              ),
                              margin: EdgeInsets.only(top: px(20)),
                              child: Stack(
                                alignment: Alignment.center,
                                children: [
                                  Image.network(
                                    '${state[i].video}?vframe/jpg/offset/0/h/300',
                                  ),
                                  Icon(
                                    Icons.play_arrow,
                                    color: Colors.white,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        // 点赞数 评论数
                        Container(
                          margin: EdgeInsets.only(top: px(20)),
                          child: Row(
                            children: <Widget>[
                              LikeButton(
                                onTap: (bool isLiked) async {
                                  return false;
                                },
                                likeBuilder: (bool isLiked) {
                                  return Image.asset(
                                    "assets/images/icon/nolikes.png",
                                    width: px(32),
                                    height: px(30),
                                  );
                                },
                                likeCount: state[i].memberFollow,
                                size: px(32),
                              ),
                              SquareComment(state[i].comcount),
                            ],
                          ),
                        ),
                        // 查看评论
                        Container(
                          margin: EdgeInsets.only(top: px(20)),
                          child: GestureDetector(
                            onTapUp: (val) {
                              goPage(context, '/dynamic', param: {
                                "id": state[i].id,
                              });
                            },
                            child: Text(
                              "查看${state[i].comcount}条评论",
                              style: TextStyle(
                                color: col("909399"),
                                fontSize: px(24),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          );
        },
      ),
    );
  }
}
